--
-- Author: shunguo.chen
-- Date: 2018-07-27 15:25:24
--
-- TheatresBossPanel
-- 奇迹歌剧院boss进度Panel

local TheatreBossRender = import(".renders.TheatreBossRender")

local ClassRef = app.mvc.createPanelClass(...)

local BOSS_NUM_MAX = 3 -- 双方boss击杀进度节点数

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    local myRenders = {0,0,0}
    local render
    for i=1,BOSS_NUM_MAX do
    	render = TheatreBossRender.create(self._ui["icon_" .. i])
    	render:setIsEnemy(false)
    	render:setData(100)
    	myRenders[i] = render
    end
    self._myRenders = myRenders

    local enemyRenders = {0,0,0}
    for i=1,BOSS_NUM_MAX do
    	render = TheatreBossRender.create(self._ui["icon_" .. (2*BOSS_NUM_MAX - i + 1)])
    	render:setIsEnemy(true)
    	render:setData(100)
    	enemyRenders[i] = render
    end
    self._enemyRenders = enemyRenders

    local myAni = self._ui.my_ani
    myAni:findChild("Panel/red"):setVisible(false)
    myAni:findChild("Panel/blue"):setVisible(true)
    -- self._myTimeline = myAni:getTimeline()
    -- self._myTimeline:gotoFrameAndPause(0)

    local enemyAni = self._ui.enemy_ani
    enemyAni:findChild("Panel/red"):setVisible(true)
    enemyAni:findChild("Panel/blue"):setVisible(false)
    -- self._enemyTimeline = enemyAni:getTimeline()
    -- self._enemyTimeline:gotoFrameAndPause(0)

    self:refresh()
end

-- data = TheatreInfo
function ClassRef:refresh()
    if self._data then
        local sessionID = self._data.sessionID
        local sessions = self._data.sessions

        for i,session in ipairs(sessions) do
            local myRender = self._myRenders[i]
            local enemyRender = self._enemyRenders[i]
            if i == sessionID then
                myRender:setData(session.myPercent)
                enemyRender:setData(session.enemyPercent)
                self._ui.my_ani:setPositionX(myRender:getPositionX())
                self._ui.enemy_ani:setPositionX(enemyRender:getPositionX())
            elseif i < sessionID then
                myRender:setData()
                enemyRender:setData()
                local isMyWin = session:isMyWin()
                myRender:setIsWin(isMyWin)
                enemyRender:setIsWin(not isMyWin)
            else
                myRender:setData(session.myPercent)
                myRender:setNumVisible(false)
                enemyRender:setData(session.enemyPercent)
                enemyRender:setNumVisible(false)
            end
        end
    end
end

function ClassRef:playPunishAction(sessionID, isMyWin, leftPer, cb)
    local render = isMyWin and self._enemyRenders[sessionID] or self._myRenders[sessionID]
    render:playReduceAction(leftPer, cb)
end

function ClassRef:updateBossHp()
    local data = self._data:getCurSession()
    local sessionID = self._data.sessionID
    local myRender = self._myRenders[sessionID]
    local enemyRender = self._enemyRenders[sessionID]
    myRender:setData(data.myPercent)
    enemyRender:setData(data.enemyPercent)
end

return ClassRef